package com.IUT.biblio;

import java.io.IOException;
import java.util.GregorianCalendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.IUT.biblio.model.*;

public class ctrlOuvrage extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public void doGet(	HttpServletRequest request, 
			HttpServletResponse response)
			throws IOException, ServletException{
		
		if(getServletConfig().getInitParameter("action").toString().equals("add")){
			request.getRequestDispatcher("../ajoutOuv.jsp").forward(request, response);
		}
		else if(getServletConfig().getInitParameter("action").toString().equals("consult")){
			request.getRequestDispatcher("../consultOuv.jsp").forward(request, response);
		}
		else{
			request.setAttribute("error", "Action interdite : " + getServletConfig().getInitParameter("action").toString());
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
		
	}

	public void doPost(     HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException{

		ServletContext context = getServletContext();
		
		if(request.getParameter("actForm")==null){
			request.setAttribute("error", "Erreur inconnue");
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		else if(request.getParameter("actForm").toString().equals("ajoutOuv")){
			
			if(request.getParameter("ISBN").isEmpty() 
			|| request.getParameter("titre").isEmpty() 
			|| request.getParameter("editeur").isEmpty() 
			|| request.getParameter("dateJ").isEmpty() 
			|| request.getParameter("dateM").isEmpty()
			|| request.getParameter("dateA").isEmpty()
			|| request.getParameter("nomA").isEmpty() 
			|| request.getParameter("prenomA").isEmpty()){
						
						request.setAttribute("error", "Tout les champs sont obligatoires !!!");
						request.getRequestDispatcher("../error.jsp").forward(request, response);
					}
					else{
						String ISBN = request.getParameter("ISBN").toString();
						String titre = request.getParameter("titre").toString();
						String editeur = request.getParameter("editeur").toString();
						String nomA = request.getParameter("nomA").toString();
						Public lePublic = Public.valueOf(request.getParameter("public").toString()); 
						String prenomA = request.getParameter("prenomA").toString();
						
						GregorianCalendar dateEdition = new GregorianCalendar(Integer.parseInt(request.getParameter("dateA").toString()),Integer.parseInt(request.getParameter("dateM").toString())-1,Integer.parseInt(request.getParameter("dateJ").toString()));
									
						Bibliotheque biblio =(Bibliotheque)context.getAttribute("biblio");
						if(biblio.unOuvrage(ISBN)!=null){
							request.setAttribute("error", "Ouvrage déjà existant.");
							request.getRequestDispatcher("../error.jsp").forward(request, response);
						}
						else{							
							Ouvrage o = new Ouvrage(ISBN, titre, editeur, dateEdition, lePublic);
							o.lierAuteur(new Auteur(prenomA,nomA));
							biblio.lierOuvrage(ISBN, o);
							request.setAttribute("texte", "Ouvrage ajouté, id : " + ISBN);
							request.getRequestDispatcher("../debut.jsp").forward(request, response);
						}
					}
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		else if(request.getParameter("actForm").toString().equals("consultOuv")){
			request.setAttribute("id",request.getParameter("id").toString());
			request.getRequestDispatcher("../consultOuv.jsp").forward(request, response);
		}
		else{
			request.setAttribute("error", "Action interdite : " + getServletConfig().getInitParameter("action").toString());
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
}

	
}
